package com.wx.entity;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import java.awt.Font;

public class ChartUtils {
    public static void applyChineseFont(JFreeChart chart) {
        // 设置标题字体
        chart.getTitle().setFont(new Font("宋体", Font.BOLD, 20));

        // 设置图例字体
        if (chart.getLegend() != null) {
            chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
        }

        // 设置轴标签字体
        if (chart.getPlot() instanceof CategoryPlot) {
            CategoryPlot plot = (CategoryPlot) chart.getPlot();
            plot.getDomainAxis().setLabelFont(new Font("宋体", Font.PLAIN, 12));
            plot.getDomainAxis().setTickLabelFont(new Font("宋体", Font.PLAIN, 12));
            plot.getRangeAxis().setLabelFont(new Font("宋体", Font.PLAIN, 12));
            plot.getRangeAxis().setTickLabelFont(new Font("宋体", Font.PLAIN, 12));
        } else if (chart.getPlot() instanceof PiePlot) {
            PiePlot plot = (PiePlot) chart.getPlot();
            plot.setLabelFont(new Font("宋体", Font.PLAIN, 12));
        }
    }

    public static void saveChartAsPNG(java.io.File file, JFreeChart chart, int width, int height) throws java.io.IOException {
        org.jfree.chart.ChartUtils.saveChartAsPNG(file, chart, width, height);
    }
}